package com.leyou.controller;

import com.leyou.ResponseData;
import com.leyou.entity.Spu;
import com.leyou.service.SpuService;
import com.leyou.service.SpuServiceDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuServiceDetail spuServiceDetail;

    /**
     * 分页查询商品
     * @param page 要查询第几页
     * @param rows 每页显示多少行数据
     * @param key 查询的内容
     * @param saleable 是否上架
     * @return
     */
    @GetMapping(path = "/spu/page")
    public ResponseData querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "10") Integer rows,
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value="saleable",defaultValue = "true") Boolean saleable) {
        try {
            // 分页查询Spu信息
            return spuService.querySpuByPage(page, rows, key, saleable);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseData.error();
    }

    /**
     * 根据spuid查询spu详情
     * @param id
     * @return
     */
    @GetMapping(path = "/spu/detail/{id}")
    public ResponseData querySpuDetailById(@RequestParam(name = "id") Long id) {
        try {
            // TODO 调用service方法
            return ResponseData.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseData.error();
    }

    /**
     * 根据spuid查询sku
     * @param id
     * @return
     */
    @GetMapping(path = "/sku/list")
    public ResponseData querySkuBySpuId(@RequestParam(name = "id") Long id) {
        try {
            // TODO 调用service方法
            return ResponseData.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseData.error();
    }

    /**
     * 根据spu的id查询spu
     * @param id
     * @return
     */
//    @GetMapping("/spu/{id}")
//    public ResponseData querySpuById(@PathVariable("id") Long id){
//        try {
//            Spu spu = this.spuService.querySpuById(id);
//            return ResponseData.ok(spu);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return ResponseData.error();
//    }
    @GetMapping("/spu/{id}")
    public ResponseEntity querySpuById(@PathVariable("id") Long id){
        try {
            Spu spu = this.spuService.querySpuById(id);
            return ResponseEntity.ok(spu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
